package com.leyou.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leyou.httpdemo.pojo.User;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: taft
 * @Date: 2018-12-9 10:54
 * <p>
 * json'转换器就只有两个功能，一个是把对象转成json，另外就是把json转为对象
 */
public class HelloJson {

    @Test
    public void test2Json() throws JsonProcessingException {
        User user = new User();
        user.setId(8L);
        user.setAge(21);
        user.setName("柳岩");
        user.setUserName("liuyan");


        //json转换工具
        ObjectMapper objectMapper = new ObjectMapper();


        //把对象转为json
        String json = objectMapper.writeValueAsString(user);

        System.out.println("json = " + json);

        List<User> userList = Arrays.asList(user, user, user, user, user);

        String listJson = objectMapper.writeValueAsString(userList);

        System.out.println("listJson = " + listJson);

        Map<String, List<User>> mapUsers = new HashMap<>();


        mapUsers.put("users1", userList);
        mapUsers.put("users2", userList);
        mapUsers.put("users3", userList);
        mapUsers.put("users4", userList);
        mapUsers.put("users5", userList);
        mapUsers.put("users6", userList);

        String mapJson = objectMapper.writeValueAsString(mapUsers);

        System.out.println("mapJson = " + mapJson);

    }

    @Test
    public void testJson2Obj() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();


        User user = objectMapper.readValue("{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}", new TypeReference<User>(){});

        System.out.println("user = " + user);

        List<User> userList = objectMapper.readValue("[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}]", new TypeReference<List<User>>() {
        });


        System.out.println("userList = " + userList);

        Map<String,List<User>> userMap = objectMapper.readValue("{\"users3\":[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}],\"users2\":[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}],\"users1\":[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}],\"users6\":[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}],\"users5\":[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}],\"users4\":[{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null},{\"id\":8,\"userName\":\"liuyan\",\"password\":null,\"name\":\"柳岩\",\"age\":21,\"sex\":null,\"birthday\":null,\"created\":null,\"updated\":null,\"note\":null}]}",new TypeReference<Map<String,List<User>>>(){});

        System.out.println("userMap = " + userMap);

    }
}
